// pages/p1/p1.js
var app = getApp()
Page({




  data: {
    postList: [],
    userInfo: [],
    age: "",

  },

  //点赞 取消赞
  like: function (e) {
    //先改变显示，再改数据
    var index = e.currentTarget.dataset.index; //获取view中的药用currentTarget
    var likes=e.currentTarget.dataset.likes;
    var postid=e.currentTarget.dataset.id;
    likes=likes+1
    // console.log(index);
    var temp1 = 'postList[' + index + '].haslike'
    var temp2 = 'postList[' + index + '].likes'
    this.setData({
      [temp1]: true,
      [temp2]:likes
    })

    wx.request({
      url: 'http://120.24.97.106:8081/selectPostLike',
      method: 'POST',
      data: {
        "postid":postid,
        "likeuserid": app.userid,
      },
      success: (res1) => {
        if(res1.data.length==0){
          wx.request({
            url: 'http://120.24.97.106:8081/savePostLike',
            method: 'POST',
            data: {
              "postid":postid,
              "likeuserid": app.userid,
              "haslike":true
            },
            success: (res2) => {
              console.log("点赞")
            }
          });
          wx.request({
            url: 'http://120.24.97.106:8081/updateLikes',
            method: 'POST',
            data: {
              "id":postid,
              "likes":likes
            }
          });
        }        
      }
    });


  },
  notlike: function (e) {
    //先改变显示，再改数据
    var index = e.currentTarget.dataset.index; //获取view中的药用currentTarget
    var likes=e.currentTarget.dataset.likes;
    var postid=e.currentTarget.dataset.id;
    likes=likes-1
    // console.log(index);
    var temp1 = 'postList[' + index + '].haslike'
    var temp2 = 'postList[' + index + '].likes'
    this.setData({
      [temp1]: false,
      [temp2]:likes
    })

    wx.request({
      url: 'http://120.24.97.106:8081/selectPostLike',
      method: 'POST',
      data: {
        "postid":postid,
        "likeuserid": app.userid,
      },
      success: (res1) => {
        if(res1.data.length!=0){
          wx.request({
            url: 'http://120.24.97.106:8081/delPostLike',
            method: 'POST',
            data: {
              "postid":postid,
              "likeuserid": app.userid,
            },
            success: (res2) => {
              console.log("取消点赞")
            }
          });
          wx.request({
            url: 'http://120.24.97.106:8081/updateLikes',
            method: 'POST',
            data: {
              "id":postid,
              "likes":likes
            }
          });
        }        
      }
    });

  },



  onLoad: function (options) {

    wx.request({
      url: 'http://120.24.97.106:8081/post',
      success: (result) => {
        // console.log(result.data)
        this.setData({
          postList: result.data
        })
        wx.request({
          url: 'http://120.24.97.106:8081/getPostLike',
          method: 'POST',
          data: {
            "likeuserid": app.userid,
          },
          success: (res2) => {
            // console.log(res2.data)
            for (var i = 0; i < res2.data.length; i++) {
              for (var j = 0; j < this.data.postList.length; j++) {
                if (res2.data[i].postid == this.data.postList[j].id) {
                  // console.log(res2.data[i].haslike)
                  var temp = 'postList[' + j + '].haslike'
                  this.setData({
                    [temp]: res2.data[i].haslike
                  })

                }
              }
            }
          }
        });
      }
    })




    //主页主题数据
    wx.request({
      url: 'http://120.24.97.106:8081/theme/getTheme',
      method: 'POST',
      data: {
        "userid": app.userid,
      },
      success: (result) => {
        if (result.data.length != 0) {
          app.selectId = result.data[0].pictureid,
            app.pictureUrl = result.data[0].pictureUrl,
            app.p2_toptexts = result.data[0].texts
        }
      }
    });
  },


  kantie: function (e) {
    var ccc = e.currentTarget.dataset.id; //获取view中的药用currentTarget
    app.postid = ccc;
    app.back_myPost = 1
    //console.log(ccc);
    wx.navigateTo({
      url: '../p1_into/p1_into',
    })
  },

  OtherUserInfo:function(e){
    var nickname = e.currentTarget.dataset.nickname;
    var avatarUrl = e.currentTarget.dataset.avatarurl;
    // console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: '../otherUserInfo/otherUserInfo?nickname='+nickname+'&avatarUrl='+avatarUrl,
    })
  },

  to_add: function () {
    wx.navigateTo({
      url: '../addPost/addPost'
    })
  },

  onShow() {
    if (app.hasAddPost) {
      //刷新页面
      app.hasAddPost = false
      this.onLoad()
      
    }



  }




})